import 'dart:async';
import 'dart:ffi' as ffi;
import 'package:flutter/material.dart';
import 'package:call/call.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter_scaless_demo/scaless_sdk.dart';
import 'package:toast/toast.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ToastContext().init(context);
    return MaterialApp(
      title: 'Scaless Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const DemoPage(title: 'Scaless Demo Home Page'),
    );
  }
}

class DemoPage extends StatefulWidget {
  const DemoPage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<DemoPage> createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  int _counter = 0;
  String txt = '准备';
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    super.initState();
    // setModuleCallBack();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          ElevatedButton(onPressed: () => _open(), child: const Text('打开断开')),
          const SizedBox(height: 20),
          ElevatedButton(onPressed: () => _close(), child: const Text('关闭端口')),
          const SizedBox(height: 20),
          ElevatedButton(
              onPressed: () => _getResult(), child: const Text('获取重量')),
          const SizedBox(height: 20),
          ElevatedButton(onPressed: () => _tare(), child: const Text('去皮')),
          const SizedBox(height: 20),
          ElevatedButton(onPressed: () => _zero(), child: const Text('归零')),
          const SizedBox(height: 20),
          ElevatedButton(
              onPressed: () => _setAutoMode(), child: const Text('主动上传模式')),
          const SizedBox(height: 20),
          ElevatedButton(
              onPressed: () => _pretare(), child: const Text('预制去皮命令')),
        ],
      ),
    ) // This trailing comma makes auto-formatting nicer for build methods.
        );
  }

  //1、打开断开
  _open() {
    if (ScalessSDK.instance.opoOpen()) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

//2、关闭端口
  _close() {
    if (ScalessSDK.instance.opoClose()) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

  //3、获取重量
  _getResult() {
    if (ScalessSDK.instance.opoGetResult(
        "sMode,sStatus,sZero,sUnit,sNetWeight,sTareWeight,sGrossWeight")) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

  //4、去皮
  _tare() {
    if (ScalessSDK.instance.opoTare()) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

  //5、归零
  _zero() {
    if (ScalessSDK.instance.opoZero()) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

  //6、设置回调函数
  void setModuleCallBack() {
    ScalessSDK.instance.opoSetModuleCallBack((info) {
      print(info.sNetWeight);
    });
  }

  //7、主动上传模式
  _setAutoMode() {
    if (ScalessSDK.instance.opoSetAutoMode()) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }

  //8、预制去皮命令
  //value：以 g 为单位
  _pretare() {
    if (ScalessSDK.instance.opoPretare(50)) {
      Toast.show("Success!");
    } else {
      Toast.show("Fail!");
    }
  }
}
